#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QMessageBox>
#include <QSettings>  //读写ini文件
#include <QFile>
#include "options.h"
#include <QtMath>
#include "qcustomplot.h"
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlQueryModel>

#include "aboutdialog.h"
#include "msa9020.h"
#include "dialogvol.h"

#include <QNetworkInterface>
#include <QDesktopWidget>
#include <QModbusDataUnit>
#include <QtMultimedia/QMediaPlayer>
#include <QtMultimedia/QMediaPlaylist>
#include <QDir>
//#include <QRandomGenerator>

#define V_REAL_CHA  101  // 101-104为CHA\CHB的浓度值地址,共4字节
#define MSA9020_COUNT 10 // 9020卡槽的数量
#define XAXIS_RANGE 1 // X轴长度，单位：小时

class QModbusClient;
class QModbusReply;
class QSoundEffect;

enum WarningSound {
    error,
    fail,
    alarm,
    warning
};

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QMediaPlayer *player;
    QMediaPlaylist *playlist;
    QList<QFileInfo> *fileInfo;
    QDir *dir;
    DialogVol *dlgvol;
    int mp3count, volume;

private slots:
    void onStateChanged(int state);
//    void setLedRxOff();
    void setLedRxOn();
//    void setLedTxOff();
    void setLedTxOn();
    void time_update(); //时间更新槽函数，状态栏显示时间
    void deleteRecoder();
    void dispPlot();
    void refreshInifile();   //接收传递过来的数据的槽
    void readReady();
    void readReadyUnitA();
    void readReadyUnitB();
    void mPlotMoveEvent(QMouseEvent *e);
    void setvol(int v);

    void on_action_openCom_triggered();
    void on_action_closeCom_triggered();
    void on_action_exit_triggered();
    void on_action_options_triggered();
    void on_plotPaused_clicked();
    void on_plotPlay_clicked();
    void on_pageEnd_clicked();
    void on_pageFrist_clicked();
    void on_pagePrevious_clicked();
    void on_pageNext_clicked();
    void on_pushButtonGoto_clicked();
    void on_pushButtonGoto_2_clicked();
    void on_pageFrist_2_clicked();
    void on_pagePrevious_2_clicked();
    void on_pageNext_2_clicked();
    void on_pageEnd_2_clicked();
    void on_spinBox_valueChanged(int arg1);
    void on_spinBox_Pages_valueChanged(int arg1);
    void on_action_about_triggered();
    void on_action_reboot_triggered();
    void on_action_halt_triggered();
    void on_action_alarm_triggered();


    void on_actionTEST_triggered();

    void on_actionVol_triggered();

    void on_action_triggered();

    void on_action_Sound_triggered();

private:
    Ui::MainWindow *ui;
    QTimer *timer, *savetimer, *clocktime, *ledRxOff, *ledTxOff;
    QModbusClient *modbusDevice;
    //QSoundEffect *wav;

    QSettings configIni;
    QLabel *ComStatic, *ClockStatic, *ledTx, *ledRx;
    QImage *img;
    const QString m_green_SheetStyle = "min-width: 16px; min-height: 16px;max-width:16px; max-height: 16px;border-radius: 8px;  border:1px solid black;background:lime";
    const QString m_grey_SheetStyle = "min-width: 16px; min-height: 16px;max-width:16px; max-height: 16px;border-radius: 8px;  border:1px solid black;background:grey";


    QFile logTxt;
    QPointer<QCPGraph> mGraphDB;
    //QSignalMapper *pMapper;
    QSqlDatabase db;
    QButtonGroup *radioButton;
    QSqlQueryModel *model, *alarmmodel;
    QCPTextElement *plotTitleName;
    QString chname = "CHA1";
    //DigitalClock *wclock;
    int pages, saveday, alarmPages;
    int modaddr[MSA9020_COUNT]; // 存储10个卡槽的modbus地址
    int modCount[MSA9020_COUNT]; // 串口发送、接收计数，用来判断通讯状态
    QString modNameA[MSA9020_COUNT], modNameB[MSA9020_COUNT]; // 存储地名
    bool plotPlay = true;

    Msa9020 m;
    void initfrom();
    void connectSlave(); // 连接MODBUS从机

    void setAlarmStatus(QString objName, bool status);
    void writeLog(QString w_r, char *buf, int len);
    bool createConnection();
    bool dbAppendRecord(quint8 serverAddress);
    void dbAppendAlarm(quint8 serverAddress);
    void dispRecod(QString chname);
    void dispAlarm();


    void readMsaUnit(int slaveAddr);//读MSA卡的单位
    void readMsaData(int slaveAddr);//读MSA卡的浓度

    void playWarningSound();

signals:
    void sendpre(bool b);
};



#endif // MAINWINDOW_H
